package cn.jhc.DaoImpl;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.Query;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import cn.jhc.DAO.UserDao;
import cn.jhc.model.User;
@Repository
public class UserDaoImpl implements UserDao {

   private SessionFactory sessionFactory;
  
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
   @Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void addUser(User user) {
		Session session=sessionFactory.getCurrentSession();
        session.save(user);
	    
	}

	@Override
	public void removeUser(User user) {
		Session session=sessionFactory.getCurrentSession();
        session.delete(user);;

	}

	@Override	
	public User findUserById(Integer id) {	
		Session session=sessionFactory.getCurrentSession();
        User user1 = (User) session.get(User.class, id);
      	return user1;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAllUser() {
		Session session=sessionFactory.getCurrentSession();	
		List<User> list = (List<User>) session.createQuery("from users").list(); 	
		return list;

	}

	@Override
	public void updateUser(User user) {
		Session session=sessionFactory.getCurrentSession();
		session.update(user);

	}


}
